//
//  MaintainViewController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/11/24.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "MaintainViewController.h"
#import "CheckShopViewController.h"
#import "BankListViewController.h"
#import "BankCodeViewController.h"

@interface MaintainViewController ()
@property (nonatomic, strong) UIScrollView *mainScroll;
@property (nonatomic, strong) UITextField *nameTF;
@property (nonatomic, strong) UITextField *accountTF;
@property (nonatomic, strong) UITextField *idCardTF;
@property (nonatomic, strong) UITextField *phoneTF;
//@property (nonatomic, strong) UITextField *codeTF;
@property (nonatomic, strong) UIButton *nextBtn;
@property (nonatomic,strong) BankCodeViewController *bankCodeVC;
@end

@implementation MaintainViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.bgNavView.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavi];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TextFieldChange) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)setNavi
{
    UIView *navi = [UIView new];
    if (@available(iOS 11.0, *)) {
        navi.frame = CGRectMake(0, TopStatusHeight_xs, SCREENWIDTH, TopBarHeight);
    } else {
        // Fallback on earlier versions
        navi.frame = CGRectMake(0, TopStatusHeight, SCREENWIDTH, TopBarHeight);
    }
    navi.backgroundColor = [UIColor clearColor];
    [self.view addSubview:navi];
    
    UILabel *titleLab = [UILabel new];
    titleLab.frame = CGRectMake(0, 30, SCREENWIDTH, 18);
    titleLab.text = @"维护店铺";
    titleLab.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:18];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = UIColorFromHex(0x343434);
    [navi addSubview:titleLab];
    
    self.mainScroll = [UIScrollView new];
    self.mainScroll.frame = CGRectMake(0, navi.bottom, SCREENWIDTH, SCREENHEIGHT-navi.bottom);
    self.mainScroll.backgroundColor = ClearColor;
    self.mainScroll.contentSize = CGSizeMake(SCREENHEIGHT, 2*SCREENHEIGHT);
    [self.view addSubview:self.mainScroll];
    
    UILabel *headLab = [UILabel new];
    headLab.frame = CGRectMake(2*leftPadding, 15, 120, 30);
    headLab.textColor = titleColor;
    headLab.font = FontSemiboldSize(22);
    headLab.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:2 and:@"绑定银行卡"];
    [self.mainScroll addSubview:headLab];
    
    //持卡人姓名
    UILabel *nameLab = [UILabel new];
    nameLab.frame = CGRectMake(headLab.left, headLab.bottom+40, 80, 12);
    [self setLabelAttri:nameLab andStr:@"持卡人姓名"];
    [self.mainScroll addSubview:nameLab];
    self.nameTF = [UITextField new];
    [self createTFandLineAndDesLab:self.nameTF andFrame:CGRectMake(nameLab.left, nameLab.bottom+12, SCREENWIDTH-80, 15) andDesStr:@"* 请尽量保持与营业执照姓名一致" and:10];
    
    //银行卡账号
    UILabel *accountLab = [UILabel new];
    accountLab.frame = CGRectMake(nameLab.left, nameLab.bottom+78, 100, 12);
    [self setLabelAttri:accountLab andStr:@"银行卡账号"];
    [self.mainScroll addSubview:accountLab];
    self.accountTF = [UITextField new];
    [self createTFandLineAndDesLab:self.accountTF andFrame:CGRectMake(accountLab.left, accountLab.bottom+12, SCREENWIDTH-80, 15) andDesStr:@"* 请输入银行卡账号" and:11];
    
    //持卡人身份账号
    UILabel *idCardLab = [UILabel new];
    idCardLab.frame = CGRectMake(nameLab.left, accountLab.bottom+78, 100, 12);
    [self setLabelAttri:idCardLab andStr:@"持卡人身份账号"];
    [self.mainScroll addSubview:idCardLab];
    self.idCardTF = [UITextField new];
    [self createTFandLineAndDesLab:self.idCardTF andFrame:CGRectMake(idCardLab.left, idCardLab.bottom+12, SCREENWIDTH-80, 15) andDesStr:@"* 请输入持卡人身份账号" and:12];
    
    //手机号
    UILabel *phoneLab = [UILabel new];
    phoneLab.frame = CGRectMake(nameLab.left, idCardLab.bottom+78, 100, 12);
    [self setLabelAttri:phoneLab andStr:@"手机号"];
    [self.mainScroll addSubview:phoneLab];
    self.phoneTF = [UITextField new];
    [self createTFandLineAndDesLab:self.phoneTF andFrame:CGRectMake(phoneLab.left, phoneLab.bottom+12, SCREENWIDTH-80, 15) andDesStr:@"* 请输入银行预留手机号码" and:13];
    
    //验证码
    //    UILabel *codeLab = [UILabel new];
    //    codeLab.frame = CGRectMake(nameLab.left, phoneLab.bottom+78, 100, 12);
    //    [self setLabelAttri:codeLab andStr:@"验证码"];
    //    [self.mainScroll addSubview:codeLab];
    //    self.codeTF = [UITextField new];
    //    [self createTFandLineAndDesLab:self.codeTF andFrame:CGRectMake(codeLab.left, codeLab.bottom+12, SCREENWIDTH-80, 15) andDesStr:@"* 请输入手机验证码" and:14];
    
    CGFloat bottom = phoneLab.bottom+48+40;
    UIButton * nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    nextBtn.frame = CGRectMake(nameLab.left, bottom, SCREENWIDTH-80, 50);
    //    nextBtn.userInteractionEnabled = NO;
    //    nextBtn.backgroundColor = UIColorFromHex(0xD1D1D1);
    //    [nextBtn setTitleColor:UIColorFromHex(0x8D8D8D) forState:UIControlStateNormal];
    [nextBtn setAttributedTitle:[TTKUtil getAtttributeStringWithLineSpace:0 kern:1.5 and:@"下一步"] forState:UIControlStateNormal];
    nextBtn.layer.cornerRadius = 20;
    nextBtn.layer.masksToBounds = YES;
    nextBtn.backgroundColor = goldColor;
    [nextBtn setTitleColor:titleColor forState:UIControlStateNormal];
    [nextBtn addTarget:self action:@selector(nextBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.nextBtn = nextBtn;
    [self.mainScroll addSubview:nextBtn];
    self.mainScroll.contentSize = CGSizeMake(SCREENWIDTH, nextBtn.bottom+30);
}

#pragma mark -- 下一步
- (void)nextBtn:(UIButton *)btn
{
    if ([TTKUtil isNil:self.nameTF.text]) {
        [self ShowHUDWithMessage:@"持卡人姓名不能为空"];
        return;
    }
    if ([TTKUtil isNil:self.accountTF.text]) {
        [self ShowHUDWithMessage:@"银行卡账号不能为空"];
        return;
    }
    if ([TTKUtil isNil:self.idCardTF.text]) {
        [self ShowHUDWithMessage:@"持卡人身份证账号不能为空"];
        return;
    }
    if ([TTKUtil isNil:self.phoneTF.text]) {
        [self ShowHUDWithMessage:@"手机号不能为空"];
        return;
    }
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSString *name = self.nameTF.text;
    NSString *phone = self.phoneTF.text;
    [dict setValue:name forKey:@"ownerName"];//持卡人姓名
    [dict setValue:self.accountTF.text forKey:@"cardNumber"];//银行卡账号
    [dict setValue:self.idCardTF.text forKey:@"idNumber"];//持卡人身份证账号
    [dict setValue:phone forKey:@"reservedMobile"];//手机号
    [HttpRequest formPostPath:RegistBlindCard_API params:dict resultBlock:^(id Data, MsgModel *msgModel) {
        if (msgModel.status == 200) {
            __weak __typeof(self) weakSelf = self;
            self.bankCodeVC = [[BankCodeViewController alloc]init];
            [self.view addSubview:self.bankCodeVC.view];
            //[[UIApplication sharedApplication].windows[0] addSubview:self.pickVC.view];
            self.bankCodeVC.view.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
            self.bankCodeVC.callBack = ^(NSString * _Nonnull location)
            {
                //CheckShopViewController *checkVC = [CheckShopViewController new];
                //[weakSelf.navigationController pushViewController:checkVC animated:YES];
                BasicWebViewController *basicVC = [BasicWebViewController new];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [HttpRequest formPostPath:GetSignUrl_API params:nil resultBlock:^(id Data, MsgModel *msgModel)
                     {
                         dispatch_async(dispatch_get_main_queue(), ^{
                             basicVC.url = Data[@"result"];
                             [weakSelf.navigationController pushViewController:basicVC animated:YES];
                         });
                     }];
                });
            };
        }
        
    }];
    /*
     __weak __typeof(self) weakSelf = self;
     self.bankCodeVC = [[BankCodeViewController alloc]init];
     [self.view addSubview:self.bankCodeVC.view];
     //[[UIApplication sharedApplication].windows[0] addSubview:self.pickVC.view];
     self.bankCodeVC.view.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
     self.bankCodeVC.callBack = ^(NSString * _Nonnull location)
     {
     CheckShopViewController *checkVC = [CheckShopViewController new];
     [weakSelf.navigationController pushViewController:checkVC animated:YES];
     };
     */
}

#pragma mark -- 获取label
- (void)setLabelAttri:(UILabel *)label andStr:(NSString *)str
{
    label.textColor = titleColor;
    label.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:12];
    label.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:str];
}

#pragma mark -- 创建tf和des
- (void)createTFandLineAndDesLab:(UITextField*)tf andFrame:(CGRect)frame andDesStr:(NSString *)des and:(NSInteger)tag
{
    tf.frame = frame;
    tf.textColor = titleColor;
    tf.font = FontWithSize(15);
    [self.mainScroll addSubview:tf];
    
    UIView *lineView = [UIView new];
    lineView.frame = CGRectMake(tf.left, tf.bottom+4, SCREENWIDTH-80, 1);
    lineView.backgroundColor = UIColorFromHex(0xE3E3E3);
    [self.mainScroll addSubview:lineView];
    
    UILabel *desLab = [UILabel new];
    desLab.frame = CGRectMake(tf.left, lineView.bottom+5, SCREENWIDTH-tf.left*2, 10);
    desLab.font = FontWithSize(10);
    desLab.textColor = UIColorFromHex(0x828282);
    desLab.text = des;
    [self.mainScroll addSubview:desLab];
    
    if (tag == 11) {
        UILabel *showLab = [UILabel new];
        showLab.frame = CGRectMake(tf.width-60, 3, 60, 10);
        showLab.textColor = UIColorFromHex(0x3CB1FA);
        showLab.font = FontWithSize(10);
        showLab.text = @"查看支持银行";
        [tf addSubview:showLab];
        
        UIImageView *quesIV = [UIImageView new];
        quesIV.frame = CGRectMake(showLab.left-5-9, showLab.top+1, 9, 9 );
        quesIV.image = [UIImage imageNamed:@"question"];
        [tf addSubview:quesIV];
        
        UIButton *quesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        quesBtn.frame = CGRectMake(quesIV.left, 0, 74, tf.height);
        quesBtn.backgroundColor = [UIColor clearColor];
        [quesBtn addTarget:self action:@selector(questionList:) forControlEvents:UIControlEventTouchUpInside];
        [tf addSubview:quesBtn];
        //tf.width = quesIV.left-40;
    }
    else if (tag == 14)
    {
        //        UIButton *codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        //        codeBtn.frame = CGRectMake(tf.width-65, 0, 65, 10);
        //        codeBtn.backgroundColor = TotalWhite;
        //        [codeBtn setTitleColor:UIColorFromHex(0x828282) forState:UIControlStateNormal];
        //        codeBtn.titleLabel.font = FontWithSize(10);
        //        [codeBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
        //        [codeBtn addTarget:self action:@selector(codeBtn:) forControlEvents:UIControlEventTouchUpInside];
        //        [tf addSubview:codeBtn];
        //
        //        UIView *lineView = [UIView new];
        //        lineView.frame = CGRectMake(codeBtn.left-1, 0, 1, 10);
        //        lineView.backgroundColor = UIColorFromHex(0x828282);
        //        [tf addSubview:lineView];
    }
}

#pragma mark -- 银行列表
- (void)questionList:(UIButton *)btn
{
    BankListViewController *bankList = [BankListViewController new];
    [self.navigationController pushViewController:bankList animated:YES];
}

#pragma mark -- tf
- (void)TextFieldChange
{
    NSString *idStr = self.idCardTF.text;
    NSString *bankCode = self.accountTF.text;
    NSString *phone = self.phoneTF.text;
    if (idStr.length > 18) {
        self.idCardTF.text = [self.idCardTF.text substringToIndex:18];
    }
    if (bankCode.length > 19) {
        self.accountTF.text = [self.accountTF.text substringToIndex:19];
    }
    if (phone.length > 11) {
        self.phoneTF.text = [self.phoneTF.text substringToIndex:11];
    }
    
}

@end
